{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Check If It Is a Straight Line"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #geometry #array #math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #几何 #数组 #数学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: checkStraightLine"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #缀点成线"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定一个数组&nbsp;<code>coordinates</code>&nbsp;，其中&nbsp;<code>coordinates[i] = [x, y]</code>&nbsp;，<meta charset=\"UTF-8\" />&nbsp;<code>[x, y]</code>&nbsp;表示横坐标为 <code>x</code>、纵坐标为 <code>y</code>&nbsp;的点。请你来判断，这些点是否在该坐标系中属于同一条直线上。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong class=\"example\">示例 1：</strong></p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/10/19/untitled-diagram-2.jpg\" /></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]\n",
    "<strong>输出：</strong>true\n",
    "</pre>\n",
    "\n",
    "<p><strong class=\"example\">示例 2：</strong></p>\n",
    "\n",
    "<p><strong><img alt=\"\" src=\"https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/10/19/untitled-diagram-1.jpg\" /></strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]\n",
    "<strong>输出：</strong>false\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>2 &lt;=&nbsp;coordinates.length &lt;= 1000</code></li>\n",
    "\t<li><code>coordinates[i].length == 2</code></li>\n",
    "\t<li><code>-10^4 &lt;=&nbsp;coordinates[i][0],&nbsp;coordinates[i][1] &lt;= 10^4</code></li>\n",
    "\t<li><code>coordinates</code>&nbsp;中不含重复的点</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [check-if-it-is-a-straight-line](https://leetcode.cn/problems/check-if-it-is-a-straight-line/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [check-if-it-is-a-straight-line](https://leetcode.cn/problems/check-if-it-is-a-straight-line/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]', '[[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        x1,y1 = coordinates[0]\n",
    "        x2,y2 = coordinates[1]\n",
    "        for co in coordinates[2:]:\n",
    "            if (x2-x1) * (co[1]-y1) != (y2-y1) * (co[0]-x1):\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates) -> bool:\n",
    "        x0, y0 = coordinates[0]\n",
    "        x1, y1 = coordinates[1]\n",
    "        for idx in range(2, len(coordinates)):\n",
    "            x2, y2 = coordinates[idx]\n",
    "            if (x2-x0)*(y1-y0)-(x1-x0)*(y2-y0) != 0:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        if coordinates[1][0]-coordinates[0][0]==0:\n",
    "            k=inf\n",
    "        else:\n",
    "            k=(coordinates[1][1]-coordinates[0][1])/(coordinates[1][0]-coordinates[0][0])\n",
    "        for i in range(2,len(coordinates)):\n",
    "            if coordinates[i][0]-coordinates[i-1][0]==0:\n",
    "                t=inf\n",
    "            else:\n",
    "                t=(coordinates[i][1]-coordinates[i-1][1])/(coordinates[i][0]-coordinates[i-1][0])\n",
    "            if t!=k: return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "\n",
    "        x0, y0, x1, y1 = coordinates[0][0], coordinates[0][1], coordinates[1][0], coordinates[1][1]\n",
    "        for ii in range(2, len(coordinates)):\n",
    "            if (coordinates[ii][0] - x1) * (y1 - y0) != (coordinates[ii][1] - y1) * (x1 - x0):\n",
    "                return False\n",
    "        \n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, A: List[List[int]]) -> bool:\n",
    "        seen = set()\n",
    "        for [x1, y1], [x2, y2] in zip(A, A[1:]):\n",
    "            if x1 == x2: k = '无穷'\n",
    "            else: k = (y2-y1) / (x2-x1)\n",
    "            seen.add(k)\n",
    "            if len(seen) == 2: return False\n",
    "\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        if coordinates[0][0]-coordinates[1][0]==0:\n",
    "            for i in range(2,len(coordinates)):\n",
    "                if coordinates[i][0]!=coordinates[0][0]:\n",
    "                    return False\n",
    "            return True\n",
    "        if coordinates[0][1]-coordinates[1][1]==0:\n",
    "            for i in range(2,len(coordinates)):\n",
    "                if coordinates[i][1]!=coordinates[0][1]:\n",
    "                    return False\n",
    "            return True\n",
    "        k = (coordinates[0][1]-coordinates[1][1])/(coordinates[0][0]-coordinates[1][0])\n",
    "        b = coordinates[0][1]-coordinates[0][0]*k\n",
    "        print(k,b)\n",
    "        for i in range(2,len(coordinates)):\n",
    "            if coordinates[i][1]!=k*coordinates[i][0]+b:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        try:\n",
    "            for i in range(2,len(coordinates)):\n",
    "                assert (coordinates[i][0]-coordinates[0][0])*(coordinates[1][1]-coordinates[0][1]) == (coordinates[i][1]-coordinates[0][1])*(coordinates[1][0]-coordinates[0][0])\n",
    "        except:\n",
    "            return False\n",
    "\n",
    "        return True \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        wset = set()\n",
    "        for [x1, y1], [x2, y2] in zip(coordinates, coordinates[1:]):\n",
    "            if x1 == x2: k = '无穷'\n",
    "            else: k = (y2-y1) / (x2-x1)\n",
    "            wset.add(k)\n",
    "            if len(wset) == 2: \n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        \"\"\"1.首先如果只有两点那么肯定共线    2.如果两点以上，则后面的数每次与前者进行判断，向量乘积结果为‘0’时说明三点共线 . 例如：       已知三点A(1,2),B(3,4),C(6,7)，求这三点是否在—条直线?        解：           向量AB = (3，4)−(1，2)＝(2，2)           向量AC = (6，7)−(3，4)＝(3，3)           向量AB × 向量AC = (2 × 3) - (3 × 2) = 0           所以AB和AC平行           所以A,B,C三点共线 . 例题理解后1037、1232利用这个思路进行解题即可\n",
    "        \"\"\"\n",
    "        for i in range(2, len(coordinates)):\n",
    "            res1 = [coordinates[i-1][0] - coordinates[i-2][0],\n",
    "                    coordinates[i-1][1] - coordinates[i-2][1]\n",
    "            ]\n",
    "            res2 = [coordinates[i][0] - coordinates[i-2][0],\n",
    "                    coordinates[i][1] - coordinates[i-2][1],\n",
    "            ]\n",
    "            if (res1[0] * res2[1]) - (res1[1]*res2[0]) != 0:\n",
    "                return False\n",
    "        return True\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        n1 = coordinates[1][0] - coordinates[0][0]\n",
    "        n2 = coordinates[1][1] - coordinates[0][1]\n",
    "        for x, y in coordinates[2:]:\n",
    "            if n1 * (y - coordinates[0][1]) != n2 * (x - coordinates[0][0]):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "  def checkStraightLine(self, coordinates: list[list[int]]) -> bool:\n",
    "\n",
    "    \n",
    "    x_0=coordinates[1][0]-coordinates[0][0]\n",
    "    y_0=coordinates[1][1]-coordinates[0][1]\n",
    "    ans=True\n",
    "\n",
    "    i=0\n",
    "    while(i<len(coordinates)):\n",
    "      if((coordinates[i][1]-coordinates[0][1])*x_0 != y_0*(coordinates[i][0]-coordinates[0][0])):\n",
    "        return False\n",
    "      i+=1\n",
    "    return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        seen=set()\n",
    "        for [x1,y1],[x2,y2] in zip(coordinates,coordinates[1:]):\n",
    "            if x1==x2:\n",
    "                k='无穷'\n",
    "            else:\n",
    "                k=(y2-y1)/(x2-x1)\n",
    "            seen.add(k)\n",
    "            if len(seen)!=1:\n",
    "                return False\n",
    "        return True\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        x0,y0,x1,y1 = coordinates[0][0],coordinates[0][1],coordinates[1][0],coordinates[1][1]\n",
    "        n = len(coordinates)\n",
    "        if x0 != x1:\n",
    "            k = (y0 - y1) / (x0 - x1)\n",
    "            b = y0 - k * x0\n",
    "            for i in range(2,n):\n",
    "                x_y = coordinates[i]\n",
    "                x,y = x_y[0],x_y[1]\n",
    "                if y != k * x + b:\n",
    "                    return False\n",
    "            return True\n",
    "        else:\n",
    "            for i in range(2,n):\n",
    "                x_y = coordinates[i]\n",
    "                x = x_y[0]\n",
    "                if x != x0:\n",
    "                    return False\n",
    "            return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        return all((coordinates[1][0] - coordinates[0][0])*(y - coordinates[0][1]) == (coordinates[1][1] - coordinates[0][1])* (x - coordinates[0][0])  for x,y in coordinates[2:])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        flag = False\n",
    "        if coordinates[1][0]- coordinates[0][0] == 0:\n",
    "            flag = True\n",
    "        else:\n",
    "            rate = (coordinates[1][1]-coordinates[0][1])/(coordinates[1][0]-coordinates[0][0])\n",
    "        for i in range(2,len(coordinates)):\n",
    "            if flag==True:\n",
    "                if coordinates[i][0]-coordinates[i-1][0] != 0:\n",
    "                    return False\n",
    "            else:\n",
    "                if (coordinates[i][0]-coordinates[i-1][0])== 0:\n",
    "                    return False\n",
    "                elif (coordinates[i][1]-coordinates[i-1][1])/(coordinates[i][0]-coordinates[i-1][0]) != rate:\n",
    "                    return False\n",
    "        return True\n",
    "\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        for coord in coordinates:\n",
    "            temp1 = (coordinates[-1][0] - coordinates[0][0])*(coord[1] - coordinates[0][1])\n",
    "            temp2 = (coord[0] - coordinates[0][0])*(coordinates[-1][1] - coordinates[0][1])\n",
    "            if temp1 != temp2:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        flag = False\n",
    "        if len(coordinates) == 2:\n",
    "            return True\n",
    "        elif coordinates[0][0] == coordinates[1][0]:\n",
    "            flag = True\n",
    "        for i in range(2, len(coordinates)):\n",
    "            if flag == True:\n",
    "                if coordinates[i][0] != coordinates[1][0]:\n",
    "                    return False\n",
    "            else:\n",
    "                if coordinates[i][1] - coordinates[0][1] != ((coordinates[0][1] - coordinates[1][1]) / (coordinates[0][0] - coordinates[1][0])) *(coordinates[i][0] - coordinates[0][0]):\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        return all((coordinates[1][0]-coordinates[0][0])*(y-coordinates[0][1]) == (coordinates[1][1]-coordinates[0][1])*(x-coordinates[0][0]) for x,y in coordinates[2:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        return all(((coordinates[1][1] - coordinates[0][1]) * (x - coordinates[0][0]) == \n",
    "        (y - coordinates[0][1]) * (coordinates[1][0] - coordinates[0][0])) for x,y in coordinates[2:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        # 三点共线的判定: (y3−y1)(x2−x1) = (y2−y1)(x3−x1)\n",
    "        delta_1 = coordinates[1][0] - coordinates[0][0]  # x2 - x1\n",
    "        delta_2 = coordinates[1][1] - coordinates[0][1]  # y2 - y1\n",
    "        \n",
    "        for location in range(2, len(coordinates)):\n",
    "            value_left = (coordinates[location][1] - coordinates[0][1]) * delta_1\n",
    "            value_right = (coordinates[location][0] - coordinates[0][0]) * delta_2\n",
    "            if value_left != value_right:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        x1 = coordinates[0][0]\n",
    "        y1 = coordinates[0][1]\n",
    "        x2 = coordinates[1][0]\n",
    "        y2 = coordinates[1][1]\n",
    "        if x2 != x1:\n",
    "            k = (y2 - y1) / (x2 - x1)\n",
    "            b = y1 - k * x1\n",
    "            for i in range(2,len(coordinates)):\n",
    "                if coordinates[i][1] != k * coordinates[i][0] + b:\n",
    "                    return False\n",
    "        else:\n",
    "            for i in range(2,len(coordinates)):\n",
    "                if coordinates[i][0] != coordinates[0][0]:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        if len(coordinates)==2:return True\n",
    "        for i in range(len(coordinates)-2):\n",
    "            if (coordinates[i+2][0]-coordinates[i+1][0])*(coordinates[i][1]-coordinates[i+1][1]) != (coordinates[i][0]-coordinates[i+1][0])*(coordinates[i+2][1]-coordinates[i+1][1]):\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        if(coordinates[1][0]-coordinates[0][0]==0): #分母为0，则所有分母为0，否则返回False\n",
    "            for i in range(len(coordinates)-1):\n",
    "                if(coordinates[i+1][0]-coordinates[i][0]!=0):\n",
    "                    return False\n",
    "            return True\n",
    "        else: #分母不为0，则记录下不为0的值，再比较\n",
    "            a=(coordinates[1][1]-coordinates[0][1])/(coordinates[1][0]-coordinates[0][0])\n",
    "            for i in range(len(coordinates)-1):\n",
    "                if(coordinates[i+1][0]-coordinates[i][0]==0):\n",
    "                    return False\n",
    "                if((coordinates[i+1][1]-coordinates[i][1])/(coordinates[i+1][0]-coordinates[i][0])!=a):\n",
    "                    return False\n",
    "            return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        n=len(coordinates)\n",
    "        if n==2:\n",
    "            return True\n",
    "        i=0\n",
    "        while i<n-2:\n",
    "            if (coordinates[i+1][0]-coordinates[i][0])*(coordinates[i+2][1]-coordinates[i+1][1]) != (coordinates[i+1][1]-coordinates[i][1])*(coordinates[i+2][0]-coordinates[i+1][0]):\n",
    "                return False\n",
    "            i=i+1\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "\n",
    "        if len(coordinates) == 2:\n",
    "            return True\n",
    "        \n",
    "        else:\n",
    "\n",
    "            base_vector = (\n",
    "                coordinates[1][0] - coordinates[0][0],\n",
    "                coordinates[1][1] - coordinates[0][1]\n",
    "            )\n",
    "\n",
    "            if base_vector[0] == 0:\n",
    "\n",
    "                for i in range(2, len(coordinates)):\n",
    "                    if coordinates[i][0] != coordinates[0][0]:\n",
    "                        return False\n",
    "                return True\n",
    "\n",
    "            for i in range(2, len(coordinates)):\n",
    "\n",
    "                vector = (\n",
    "                    coordinates[i][0] - coordinates[0][0],\n",
    "                    coordinates[i][1] - coordinates[0][1]\n",
    "                )\n",
    "\n",
    "                if vector[1] * base_vector[0] != vector[0] * base_vector[1]:\n",
    "                    return False\n",
    "            \n",
    "            return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        def calculate(p1, p2):\n",
    "            return (p2[1] - p1[1]) // (p2[0] - p1[0]) if p2[0] - p1[0] != 0 else 1e4\n",
    "        slope = calculate(coordinates[0], coordinates[1])\n",
    "        for i in range(2, len(coordinates)):\n",
    "            if calculate(coordinates[i], coordinates[i-1])!= slope:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "\n",
    "        delX = coordinates[0][0]\n",
    "        delY = coordinates[0][1]\n",
    "        \n",
    "        n = len(coordinates)\n",
    "        for i in range(n):\n",
    "            coordinates[i][0] -= delX\n",
    "            coordinates[i][1] -= delY\n",
    "        \n",
    "        A = coordinates[1][1]\n",
    "        B = -coordinates[1][0]\n",
    "        for i in range(2,n):\n",
    "            if  coordinates[i][0] * A + coordinates[i][1] *B :\n",
    "                return False\n",
    "        \n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        f=lambda x,y:(x-coordinates[0][0])*(coordinates[1][1]-coordinates[0][1])!=(y-coordinates[0][1])*(coordinates[1][0]-coordinates[0][0])\n",
    "        for i,j in coordinates[2:]:\n",
    "            if f(i,j):\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: list[list[int]]) -> bool:\n",
    "        '''\n",
    "        判断点是否在同一条直线上\n",
    "        :param coordinates:\n",
    "        :return:\n",
    "        '''\n",
    "        x = coordinates[1][0] - coordinates[0][0]\n",
    "        y = coordinates[1][1] - coordinates[0][1]\n",
    "        for i in range(2, len(coordinates)):\n",
    "            xx = coordinates[i][0] - coordinates[0][0]\n",
    "            yy = coordinates[i][1] - coordinates[0][1]\n",
    "            # 判断斜率相等\n",
    "            if x*yy != y*xx: return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        flag=\"\"\n",
    "        if coordinates[1][0]-coordinates[0][0]!=0:\n",
    "            k=(coordinates[1][1]-coordinates[0][1])/(coordinates[1][0]-coordinates[0][0])\n",
    "        else:\n",
    "            flag=\"stright\"\n",
    "        for i in range(1,len(coordinates)-1):\n",
    "            if coordinates[i+1][0]-coordinates[i][0]!=0:\n",
    "                temp=(coordinates[i+1][1]-coordinates[i][1])/(coordinates[i+1][0]-coordinates[i][0])\n",
    "            else:\n",
    "                temp=\"stright\"\n",
    "            if flag !=\"stright\":\n",
    "                if temp!=k or temp==\"stright\":\n",
    "                    return False\n",
    "            else:\n",
    "                if temp!=\"stright\":\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        p=[(node1[0]-node2[0])/(node1[1]-node2[1]) if (node1[1]-node2[1])!=0 else 'a' for node1,node2 in zip(coordinates,coordinates[1:])]\n",
    "        return len(set(p))==1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "\n",
    "        n = len(coordinates)\n",
    "        if n==2:\n",
    "            return True\n",
    "\n",
    "        coordinates = sorted(coordinates, key=lambda x:x[0])\n",
    "        x,y = coordinates[0]\n",
    "        a,b = coordinates[1]\n",
    "        d1, d2 = (y-b),(x-a) \n",
    "\n",
    "        for i in range(2,n):\n",
    "            x,y = coordinates[i-1]\n",
    "            a,b = coordinates[i]\n",
    "            c1, c2 = (y-b),(x-a) \n",
    "            \n",
    "            if d1*c2!=d2*c1:\n",
    "                return False \n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        if len(coordinates) == 2:\n",
    "            return True\n",
    "        if coordinates[1][0] == coordinates[0][0]:\n",
    "            for point in coordinates[2:]:\n",
    "                if point[0] != coordinates[0][0]:\n",
    "                    return False\n",
    "        else:\n",
    "            k = (coordinates[1][1] - coordinates[0][1]) / (coordinates[1][0] - coordinates[0][0])\n",
    "            d = coordinates[0][1] - k * coordinates[0][0]\n",
    "            for point in coordinates[2:]:\n",
    "                if point[1] != k * point[0] + d:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "\n",
    "        yy = coordinates[1][1]-coordinates[0][1]\n",
    "        xx = coordinates[1][0]-coordinates[0][0]\n",
    "        if len(coordinates)<=2:\n",
    "                return True\n",
    "        if xx!=0 :\n",
    "            a = yy/xx\n",
    "        else:\n",
    "            for i in coordinates:\n",
    "                if i[0] !=coordinates[0][0]:\n",
    "                    return False\n",
    "            return True\n",
    "        b = coordinates[0][1]-a*coordinates[0][0]\n",
    "            \n",
    "        for i in range(2,len(coordinates)):\n",
    "            sum = a*coordinates[i][0]+b\n",
    "            if coordinates[i][1]!=sum:\n",
    "                return False\n",
    "        return True\n",
    "                \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates) -> bool:\n",
    "        points_len = len(coordinates)\n",
    "        for index in range(points_len):\n",
    "            if index + 2 < points_len:\n",
    "                x1 = coordinates[index][0]\n",
    "                y1 = coordinates[index][1]\n",
    "                x2 = coordinates[index + 1][0]\n",
    "                y2 = coordinates[index + 1][1]\n",
    "                x3 = coordinates[index + 2][0]\n",
    "                y3 = coordinates[index + 2][1]\n",
    "                if (y3 - y2) * (x2 - x1) != (y2 - y1) * (x3 - x2):\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        m = len(coordinates)\n",
    "        if m <= 2:\n",
    "            return True\n",
    "                # Checking if the line is vertical\n",
    "        if (coordinates[1][0] - coordinates[0][0]) == 0:\n",
    "            for i in range(2, m):\n",
    "                if coordinates[i][0] != coordinates[0][0]:\n",
    "                    return False\n",
    "            return True\n",
    "        \n",
    "        k = (coordinates[1][1] - coordinates[0][1])/(coordinates[1][0] - coordinates[0][0])\n",
    "        b = coordinates[1][1] - k * coordinates[1][0]\n",
    "        for i in range(2, m):\n",
    "            if abs(coordinates[i][1] - (k * coordinates[i][0] + b)) > 1e-9:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        n=len(coordinates)\n",
    "        dx,dy=coordinates[1][0]-coordinates[0][0],coordinates[1][1]-coordinates[0][1]\n",
    "        for i in range(2,n):\n",
    "            nx,ny=coordinates[i][0]-coordinates[i-1][0],coordinates[i][1]-coordinates[i-1][1]\n",
    "            if nx*dy!=ny*dx:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        n1 = coordinates[1][0] - coordinates[0][0]\n",
    "        n2 = coordinates[1][1] - coordinates[0][1]\n",
    "        for x, y in coordinates:\n",
    "            if n1 * (y - coordinates[0][1]) != n2 * (x - coordinates[0][0]):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def checkStraightLine(self, coordinates):\n",
    "        point1 = coordinates[0]\n",
    "        point2 = coordinates[1]\n",
    "        if point1[0] - point2[0] == 0:\n",
    "            x = point1[0]\n",
    "            if all([point[0] == x for point in coordinates]):\n",
    "                return True\n",
    "            return False\n",
    "        if point1[1] - point2[1] == 0:\n",
    "            y = point1[1]\n",
    "            if all([point[1] == y for point in coordinates]):\n",
    "                return True\n",
    "            return False\n",
    "        k = (point1[1] - point2[1]) / (point1[0] - point2[0])\n",
    "        b = point1[1] - point1[0] * k\n",
    "        for point in coordinates:\n",
    "            if abs(point[0] * k + b - point[1]) > 10 ** -10:\n",
    "                return False\n",
    "\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\r\n",
    "        n = len(coordinates)\r\n",
    "        for i in range(2, n):\r\n",
    "            x3, y3 = coordinates[i]\r\n",
    "            x2, y2 = coordinates[i-1]\r\n",
    "            x1, y1 = coordinates[i-2]\r\n",
    "            if (y2-y1)*(x3-x2) != (y3-y2)*(x2-x1):\r\n",
    "                return False\r\n",
    "        return True \r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        n = len(coordinates)\n",
    "        for i in range(2, n):\n",
    "            if (coordinates[i][0]-coordinates[0][0])*(coordinates[1][1]-coordinates[0][1]) != (coordinates[1][0]-coordinates[0][0])*(coordinates[i][1]-coordinates[0][1]):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        # y = kx+b\n",
    "        x0, y0 = coordinates[0]\n",
    "        x1, y1 = coordinates[1]\n",
    "        for i in range(2, len(coordinates)):\n",
    "            xi, yi = coordinates[i]\n",
    "            temp = (x1 - x0) * (yi - y0)\n",
    "            temp2 = (xi - x0) * (y1 - y0)\n",
    "            # print(coordinates[i])\n",
    "            if temp2 != temp:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        #数学，解析几何\n",
    "        if coordinates == 2:\n",
    "            return True\n",
    "        else:\n",
    "            if coordinates[0][0] - coordinates[1][0] != 0:\n",
    "                k = (coordinates[0][1] - coordinates[1][1]) / (coordinates[0][0] - coordinates[1][0])\n",
    "                b = coordinates[0][1] - k * coordinates[0][0]\n",
    "                for i in range (2, len(coordinates)):\n",
    "                    if coordinates[i][1] != coordinates[i][0] * k + b:\n",
    "                        return False\n",
    "                return  True\n",
    "            else:\n",
    "                x = coordinates[0][0]\n",
    "                for i in range (2, len(coordinates)):\n",
    "                    if coordinates[i][0] != x:\n",
    "                        return False\n",
    "                return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from itertools import pairwise\n",
    "coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: list[list[int]]) -> bool:\n",
    "        if(len(coordinates)<1):\n",
    "            return False\n",
    "        l=[]\n",
    "        for a,b in  pairwise(coordinates):\n",
    "            if(b[0]-a[0]==0):\n",
    "                l.append('a')\n",
    "            else:\n",
    "                l.append((b[1]-a[1])/(b[0]-a[0]))\n",
    "    \n",
    "        return len(set(l))==1 \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        p=[(node1[0]-node2[0])/(node1[1]-node2[1]) if (node1[1]-node2[1])!=0 else 'a' for node1,node2 in zip(coordinates,coordinates[1:])]\n",
    "        return len(set(p))==1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        if len(coordinates) < 2:\n",
    "            return True\n",
    "        x0,y0 = coordinates[0]\n",
    "        x1,y1 = coordinates[1]\n",
    "\n",
    "        for i in range(2,len(coordinates)):\n",
    "            x,y = coordinates[i]\n",
    "            if (y-y0)*(x-x1) != (y-y1)*(x-x0):\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, nums: List[List[int]]) -> bool:\n",
    "        x1, y1 = nums[0]\n",
    "        x2, y2 = nums[1]\n",
    "        slope = (y2- y1)/(x2 - x1) if x2 != x1 else float('inf')\n",
    "\n",
    "        for i in range(2, len(nums)):\n",
    "            xi, yi = nums[i]\n",
    "            sl = (yi - y1)/(xi - x1) if xi != x1 else float('inf')\n",
    "\n",
    "            if sl != slope:\n",
    "                return False\n",
    "        \n",
    "        return True\n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        return all((coordinates[1][0] - coordinates[0][0])*(y - coordinates[0][1]) == \n",
    "                 (coordinates[1][1] - coordinates[0][1])* (x - coordinates[0][0])  \n",
    "                 for x,y in coordinates[2:])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        if all(coordinates[i][0]==coordinates[0][0] for i in range(1,len(coordinates))):\n",
    "            return True\n",
    "        else:\n",
    "            if coordinates[1][0]-coordinates[0][0]==0:\n",
    "                return False\n",
    "            k = (coordinates[1][1]-coordinates[0][1])/(coordinates[1][0]-coordinates[0][0])\n",
    "            for i in range(2,len(coordinates)):\n",
    "                if coordinates[i][0]-coordinates[0][0]==0:\n",
    "                    return False\n",
    "                elif k != (coordinates[i][1]-coordinates[0][1])/(coordinates[i][0]-coordinates[0][0]):\n",
    "                    return False\n",
    "            return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        kx = coordinates[1][1] - coordinates[0][1]\n",
    "        ky = coordinates[1][0] - coordinates[0][0]\n",
    "        b = ky*coordinates[0][1] - kx*coordinates[0][0]\n",
    "        for x,y in coordinates:\n",
    "            if ky*y != (b + kx*x):\n",
    "                return False\n",
    "        return True\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        if coordinates[1][0] == coordinates[0][0]:\n",
    "            for coordinate in coordinates:\n",
    "                if coordinate[0] != coordinates[0][0]:\n",
    "                    return False\n",
    "            return True\n",
    "        \n",
    "        k = (coordinates[1][1] - coordinates[0][1])/(coordinates[1][0]-coordinates[0][0])\n",
    "\n",
    "        for coordinate in coordinates[1:]:\n",
    "            if coordinate[0] == coordinates[0][0]:\n",
    "                return False\n",
    "            if (coordinate[1]-coordinates[0][1])/(coordinate[0]-coordinates[0][0]) != k:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        if len(coordinates) == 2:\n",
    "            return True\n",
    "\n",
    "        if coordinates[0][0] == coordinates[1][0]:\n",
    "            for i in range(2, len(coordinates)):\n",
    "                if coordinates[i][0] != coordinates[0][0]:\n",
    "                    return False\n",
    "            return True\n",
    "\n",
    "        k = (coordinates[0][1] - coordinates[1][1]) / (coordinates[0][0] - coordinates[1][0])\n",
    "        h = coordinates[0][1] - k * coordinates[0][0]\n",
    "\n",
    "        for i in range(2, len(coordinates)):\n",
    "            if coordinates[i][0] * k + h != coordinates[i][1]:\n",
    "                return False\n",
    "        \n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        if len(coordinates) < 3:\n",
    "            return True\n",
    "        \n",
    "        x0, y0 = coordinates[0]\n",
    "        x1, y1 = coordinates[1]\n",
    "        \n",
    "        for i in range(2, len(coordinates)):\n",
    "            x, y = coordinates[i]\n",
    "            if (x1 - x0) * (y - y0) != (x - x0) * (y1 - y0):\n",
    "                return False\n",
    "        \n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates):\n",
    "        \"\"\"\n",
    "        判断所有点是否在同一直线之上\n",
    "        Args:\n",
    "            coordinates (list[int]): 各个点坐标\n",
    "        returns (bool): 所有点是否处于同一直线之上\n",
    "        \"\"\"\n",
    "        #处理异常情况\n",
    "        if coordinates == None or len(coordinates) <= 2:\n",
    "            return True \n",
    "\n",
    "        #根据前两个点坐标, 求解直线方程\n",
    "        x_1, x_2 = coordinates[0][0], coordinates[1][0]\n",
    "        y_1, y_2 = coordinates[0][1], coordinates[1][1]\n",
    "        if x_1 == x_2:\n",
    "            k = None \n",
    "        else:\n",
    "            k = (y_2 - y_1)/(x_2 - x_1)\n",
    "            b = y_1 - k * x_1 \n",
    "        \n",
    "        n = len(coordinates)\n",
    "\n",
    "        #遍历coordinates的后续点\n",
    "        for i in range(2, n):\n",
    "            point = coordinates[i]\n",
    "            #case1. 若不满足直线方程, 直接返回\n",
    "            if k == None:\n",
    "                if point[0] != coordinates[0][0]:\n",
    "                    return False \n",
    "            else:\n",
    "                if point[1] != k * point[0] + b:\n",
    "                    return False \n",
    "                \n",
    "        #returns\n",
    "        return True \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "\n",
    "        ans = []\n",
    "        for i in range(1,len(coordinates)):\n",
    "            if coordinates[i][0] - coordinates[i-1][0] == 0:\n",
    "                ans.append(float(\"inf\"))\n",
    "            else:\n",
    "                ans.append((coordinates[i][1]-coordinates[i-1][1])/(coordinates[i][0] -coordinates[i-1][0]))\n",
    "        return len(set(ans)) == 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        l = len(coordinates)\n",
    "        if l == 2:\n",
    "            return True\n",
    "        v1 = (coordinates[1][0] - coordinates[0][0],coordinates[1][1] - coordinates[0][1])\n",
    "        for i in range(2,l):\n",
    "            vi = (coordinates[i][0] - coordinates[0][0],coordinates[i][1] - coordinates[0][1])\n",
    "            if v1[0]*vi[1] - v1[1]*vi[0] != 0:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        if len(coordinates) == 2:\n",
    "            return True\n",
    "        else:\n",
    "            if (coordinates[1][0] - coordinates[0][0])!=0:\n",
    "                k = (coordinates[1][1]-coordinates[0][1]) / (coordinates[1][0] - coordinates[0][0])\n",
    "            else:\n",
    "                k = \"!\"\n",
    "        \n",
    "        for i in range(2, len(coordinates)):\n",
    "            if (coordinates[i][0] - coordinates[0][0])!=0:\n",
    "                _k = (coordinates[i][1]-coordinates[0][1]) / (coordinates[i][0] - coordinates[0][0])\n",
    "            else:\n",
    "                _k = \"!\"\n",
    "            if k != _k:\n",
    "                return False\n",
    "\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        n = len(coordinates)\n",
    "        x0, y0 = coordinates[0]\n",
    "        x1, y1 = coordinates[1]\n",
    "        x, y = x1-x0, y1-y0\n",
    "        for i in range(2, n):\n",
    "            xi, yi = coordinates[i][0]-x0, coordinates[i][1]-y0\n",
    "            if x*yi-y*xi: # 共线\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        n=len(coordinates)\n",
    "        for i in range(2,n):\n",
    "            if (coordinates[i][0]-coordinates[0][0])*(coordinates[1][1]-coordinates[0][1])!=(coordinates[1][0]-coordinates[0][0])*(coordinates[i][1]-coordinates[0][1]):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        l = len(coordinates)\n",
    "        Flag = True\n",
    "        if coordinates[1][1]-coordinates[0][1]==0:\n",
    "            for i in range(l-1):\n",
    "                if coordinates[i+1][1]-coordinates[i][1]==0:\n",
    "                    continue\n",
    "                else:\n",
    "                    Flag = False\n",
    "                    break\n",
    "        elif coordinates[1][0]-coordinates[0][0]==0:\n",
    "            for i in range(l-1):\n",
    "                if coordinates[i+1][0]-coordinates[i][0]==0:\n",
    "                    continue\n",
    "                else:\n",
    "                    Flag = False\n",
    "                    break\n",
    "        else:\n",
    "            a = (coordinates[1][1]-coordinates[0][1])/(coordinates[1][0]-coordinates[0][0])\n",
    "            for i in range(1,l-1):\n",
    "                if coordinates[i+1][1]-coordinates[i][1]!= 0 and coordinates[i+1][0]-coordinates[i][0] != 0 and (coordinates[i+1][1]-coordinates[i][1])/(coordinates[i+1][0]-coordinates[i][0]) == a:\n",
    "                    continue\n",
    "                else:\n",
    "                    Flag = False\n",
    "                    break\n",
    "        return Flag"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        if len(coordinates) == 2:\n",
    "            return True\n",
    "        \n",
    "        x1, y1 = coordinates[0][0], coordinates[0][1]\n",
    "        x2, y2 = coordinates[1][0], coordinates[1][1]\n",
    "\n",
    "        for i in range(2, len(coordinates)):\n",
    "            x3, y3 = coordinates[i][0], coordinates[i][1]\n",
    "            if (y3 - y2) * (x2 - x1) != (y2 - y1) * (x3 - x2):\n",
    "                return False\n",
    "        \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        x1,y1 = coordinates[0][0],coordinates[0][1]\n",
    "        x2,y2 = coordinates[1][0],coordinates[1][1]\n",
    "        for i in range(len(coordinates)):\n",
    "            x3,y3 = coordinates[i][0],coordinates[i][1]\n",
    "            if (y3-y2)*(x2-x1)!=(y2-y1)*(x3-x2):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        dx0 = coordinates[1][0] - coordinates[0][0]\n",
    "        dy0= coordinates[1][1] - coordinates[0][1]\n",
    "        for i in range(2, len(coordinates)):\n",
    "            p2 = coordinates[i]\n",
    "            p1 = coordinates[i-1]\n",
    "            dx1 = p2[0]-p1[0]\n",
    "            dy1 = p2[1]-p1[1]\n",
    "            print(dx0 * dy1-dx1*dy0)\n",
    "            print(i)\n",
    "            if dx0 * dy1-dx1*dy0 != 0:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        n1=coordinates[1][0]-coordinates[0][0]\n",
    "        n2=coordinates[1][1]-coordinates[0][1]\n",
    "        for x,y in coordinates[2:]:\n",
    "            if n1*(y-coordinates[0][1])!=n2*(x-coordinates[0][0]):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        n = len(coordinates)\n",
    "        if n == 2:\n",
    "            return True\n",
    "                \n",
    "        for x in coordinates[2:]:\n",
    "            if not self.check_line(coordinates[0], coordinates[1], x):\n",
    "                return False\n",
    "        return True\n",
    "    def check_line(self, p1, p2, p3):\n",
    "        if (p2[1] - p1[1]) * (p3[0] - p1[0]) == (p3[1] - p1[1]) * (p2[0] - p1[0]):\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        le=len(coordinates)\n",
    "        if (coordinates[le-1][0]-coordinates[0][0])==0:\n",
    "            for i in range(le):\n",
    "                if coordinates[i][0]!=coordinates[0][0]:\n",
    "                    return False\n",
    "            return True\n",
    "        k=(coordinates[le-1][1]-coordinates[0][1])/(coordinates[le-1][0]-coordinates[0][0])\n",
    "        b=coordinates[0][1]-k*coordinates[0][0]\n",
    "        for i in range(1,le):\n",
    "            if coordinates[i][1]!=k*coordinates[i][0]+b:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        return all((coordinates[1][0] - coordinates[0][0])*(y - coordinates[0][1]) == \n",
    "                 (coordinates[1][1] - coordinates[0][1])* (x - coordinates[0][0])  \n",
    "                 for x,y in coordinates[2:])\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:\n",
    "        n = len(coordinates)\n",
    "        ans = set()\n",
    "        for i in range(1, n):\n",
    "            if coordinates[i][0] - coordinates[i-1][0] == 0:\n",
    "                k = float(\"inf\")\n",
    "            else:\n",
    "                k = (coordinates[i][1] - coordinates[i-1][1])/(coordinates[i][0] - coordinates[i-1][0])\n",
    "            ans.add(k)\n",
    "        return len(ans) == 1"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
